﻿///////////////////////////////////////////////////////////////////////////////
// Copyright 2016 lingzimaike@gmail.com
// GPLv2
// Author: lingzimaike
///////////////////////////////////////////////////////////////////////////////

#ifndef PAPA_THREAD_THREAD_H_
#define PAPA_THREAD_THREAD_H_

#include <boost/scoped_ptr.hpp>
#include <papa/base.h>
#include <papa/platform.h>
#include <papa/thread/thread_runnable.h>

namespace papa {
namespace thread {

	class Thread
	{
	public:
		typedef platform::__TID_t	TID_t;
		enum State
		{
			NOT_READY,
			READY,
			RUNNING,
			EXIT
		};

		Thread(ThreadRunnable* runnable);
        Thread(::boost::scoped_ptr<ThreadRunnable>& runnable);
		~Thread(void);

		bool Start(void);
		void Join(void);
		inline static void Sleep(::time_t millisecond);

		TID_t get_tid(void) const { return tid_; }
		State get_state(void) const { return state_; }
		ThreadRunnable* get_runnable(void) { return runnable_.get(); }
		inline static TID_t get_current_tid(void);

	private:
#if defined(__WINDOWS__)
		static unsigned __stdcall thread_procedure(void* argument);
#else
#endif

		TID_t tid_;
		volatile State state_;
#if defined(__WINDOWS__)
		::uintptr_t thread_handle_;
#endif
        ::boost::scoped_ptr<ThreadRunnable> runnable_;

		PAPA_DISALLOW_COPY_AND_ASSIGN(Thread)
	};

}}  // namespace papa::thread

#endif // PAPA_THREAD_THREAD_H_
